package com.wali.modules.common.control;

import com.alibaba.fastjson.JSONObject;
import com.wali.common.ccp.exception.CcpException;
import com.wali.common.enums.ErrorCodeEnum;
import com.wali.common.web.BaseController;
import com.wali.modules.common.bo.ComAreaBO;
import com.wali.modules.common.service.ComAreaService;
import com.wali.modules.common.service.ComChannelService;
import com.wali.modules.common.service.ComStoreService;
import com.wali.modules.common.vo.ComAreaVO;
import com.wali.modules.common.vo.ComChannelVO;
import com.wali.modules.common.vo.ComStoreVO;
import com.wali.modules.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author walixu
 * @ClassName com.wali.modules.common.control
 * @Description ${todo}
 * @email wali.xu@kpmg.com
 * @date 24/01/2019 7:43 下午
 */
@Controller
@RequestMapping(value = "/com")
public class ComStoreController extends BaseController {

    @Autowired
    private ComStoreService comStoreService;

    @Autowired
    private ComChannelService comChannelService;

    @Autowired
    private ComAreaService comAreaService;

    /**
     * @param
     * @return
     * @throws
     * @Title listAllComStore
     * @Description 查询所有的仓库信息
     */
    @RequestMapping(value = "/listAllComStore", method = RequestMethod.GET)
    public ResponseEntity<String> listAllComStore(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            HttpSession session = request.getSession(false);
            SysUserVO user = (SysUserVO) session.getAttribute("user");
            if (user != null) {
                List<ComStoreVO> comStoreList = comStoreService.listComStore(user.getStoreId());
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
                return getResponseEntity(code, msg, comStoreList);
            }
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 查询所有的仓库信息、渠道信息、省份信息
     */
    @RequestMapping(value = "/listAllCom", method = RequestMethod.GET)
    public ResponseEntity<String> listAllCom(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            HttpSession session = request.getSession(false);
            SysUserVO user = (SysUserVO) session.getAttribute("user");

            if (user != null) {
                JSONObject result = new JSONObject();

                List<ComStoreVO> comStoreList = comStoreService.listComStore(user.getStoreId());
                result.put("storeList", comStoreList);

                List<ComChannelVO> comChannelList = comChannelService.listAllComChannel();
                result.put("channelList", comChannelList);

                ComAreaBO comAreaBO = new ComAreaBO();
                comAreaBO.setParentCode("CN");
                List<ComAreaVO> comAreaList = comAreaService.listComArea(comAreaBO);
                result.put("provinceList", comAreaList);

                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
                return getResponseEntity(code, msg, result);
            }
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }


}
